/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.home.modular.structure.user.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.home.modular.structure.org.entity.HomeOrg;
import vip.xiaonuo.home.modular.structure.position.entity.HomePosition;
import vip.xiaonuo.home.modular.structure.user.entity.HomeUser;
import vip.xiaonuo.home.modular.structure.user.param.*;
import vip.xiaonuo.home.modular.structure.user.result.HomeUserOwnResourceResult;
import vip.xiaonuo.home.modular.structure.user.result.HomeUserRoleResult;
import vip.xiaonuo.home.modular.structure.user.service.HomeUserService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;

import java.io.IOException;
import java.util.List;

/**
 * 人员控制器
 *
 * @author xuyuxiang
 * @date 2022/4/22 9:34
 **/
@Tag(name = "人员控制器")
@RestController
@Validated
public class HomeUserController {

    @Resource
    private HomeUserService homeUserService;

    /**
     * 获取人员分页
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取人员分页")
    @SaCheckPermission("/home/structure/user/page")
    @GetMapping("/home/structure/user/page")
    public CommonResult<Page<HomeUser>> page(HomeUserPageParam homeUserPageParam) {
        return CommonResult.data(homeUserService.page(homeUserPageParam));
    }

    /**
     * 添加人员
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:47
     */
    @Operation(summary = "添加人员")
    @CommonLog("添加人员")
    @SaCheckPermission("/home/structure/user/add")
    @PostMapping("/home/structure/user/add")
    public CommonResult<String> add(@RequestBody @Valid HomeUserAddParam homeUserAddParam) {
        homeUserService.add(homeUserAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑人员
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:47
     */
    @Operation(summary = "编辑人员")
    @CommonLog("编辑人员")
    @SaCheckPermission("/home/structure/user/edit")
    @PostMapping("/home/structure/user/edit")
    public CommonResult<String> edit(@RequestBody @Valid HomeUserEditParam homeUserEditParam) {
        homeUserService.edit(homeUserEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除人员
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "删除人员")
    @CommonLog("删除人员")
    @SaCheckPermission("/home/structure/user/delete")
    @PostMapping("/home/structure/user/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<HomeUserIdParam> homeUserIdParamList) {
        homeUserService.delete(homeUserIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取人员详情
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取人员详情")
    @SaCheckPermission("/home/structure/user/detail")
    @GetMapping("/home/structure/user/detail")
    public CommonResult<HomeUser> detail(@Valid HomeUserIdParam homeUserIdParam) {
        return CommonResult.data(homeUserService.detail(homeUserIdParam));
    }

    /**
     * 禁用人员
     *
     * @author xuyuxiang
     * @date 2021/10/13 14:01
     **/
    @Operation(summary = "禁用人员")
    @CommonLog("禁用人员")
    @SaCheckPermission("/home/structure/user/disableUser")
    @PostMapping("/home/structure/user/disableUser")
    public CommonResult<String> disableUser(@RequestBody HomeUserIdParam homeUserIdParam) {
        homeUserService.disableUser(homeUserIdParam);
        return CommonResult.ok();
    }

    /**
     * 启用人员
     *
     * @author xuyuxiang
     * @date 2021/10/13 14:01
     **/
    @Operation(summary = "启用人员")
    @CommonLog("启用人员")
    @SaCheckPermission("/home/structure/user/enableUser")
    @PostMapping("/home/structure/user/enableUser")
    public CommonResult<String> enableUser(@RequestBody @Valid HomeUserIdParam homeUserIdParam) {
        homeUserService.enableUser(homeUserIdParam);
        return CommonResult.ok();
    }

    /**
     * 重置人员密码
     *
     * @author xuyuxiang
     * @date 2021/10/13 14:01
     **/
    @Operation(summary = "重置人员密码")
    @CommonLog("重置人员密码")
    @SaCheckPermission("/home/structure/user/resetPassword")
    @PostMapping("/home/structure/user/resetPassword")
    public CommonResult<String> resetPassword(@RequestBody @Valid HomeUserIdParam homeUserIdParam) {
        homeUserService.resetPassword(homeUserIdParam);
        return CommonResult.ok();
    }

    /**
     * 人员拥有角色
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取人员拥有角色")
    @SaCheckPermission("/home/structure/user/ownRole")
    @GetMapping("/home/structure/user/ownRole")
    public CommonResult<List<String>> ownRole(@Valid HomeUserIdParam homeUserIdParam) {
        return CommonResult.data(homeUserService.ownRole(homeUserIdParam));
    }

    /**
     * 给人员授权角色
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "给人员授权角色")
    @CommonLog("给人员授权角色")
    @SaCheckPermission("/home/structure/user/grantRole")
    @PostMapping("/home/structure/user/grantRole")
    public CommonResult<String> grantRole(@RequestBody @Valid HomeUserGrantRoleParam homeUserGrantRoleParam) {
        homeUserService.grantRole(homeUserGrantRoleParam);
        return CommonResult.ok();
    }

    /**
     * 获取用户拥有资源
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取用户拥有资源")
    @SaCheckPermission("/home/structure/user/ownResource")
    @GetMapping("/home/structure/user/ownResource")
    public CommonResult<HomeUserOwnResourceResult> ownResource(@Valid HomeUserIdParam homeUserIdParam) {
        return CommonResult.data(homeUserService.ownResource(homeUserIdParam));
    }

    /**
     * 给用户授权资源
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "给用户授权资源")
    @CommonLog("给用户授权资源")
    @SaCheckPermission("/home/structure/user/grantResource")
    @PostMapping("/home/structure/user/grantResource")
    public CommonResult<String> grantResource(@RequestBody @Valid HomeUserGrantResourceParam homeUserGrantResourceParam) {
        homeUserService.grantResource(homeUserGrantResourceParam);
        return CommonResult.ok();
    }

    /**
     * 人员导出
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "人员导出")
    @CommonLog("人员导出")
    @SaCheckPermission("/home/structure/user/export")
    @GetMapping(value = "/home/structure/user/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportUser(HomeUserExportParam homeUserExportParam, HttpServletResponse response) throws IOException {
        homeUserService.exportUser(homeUserExportParam, response);
    }

    /**
     * 按模板导出人员个人信息
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "导出人员个人信息")
    @CommonLog("导出人员个人信息")
    @GetMapping(value = "/home/structure/user/exportUserInfo", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportUserInfo(HomeUserIdParam homeUserIdParam, HttpServletResponse response) throws IOException {
        homeUserService.exportUserInfo(homeUserIdParam, response);
    }

    /* ====人员部分所需要用到的选择器==== */

    /**
     * 获取机构树选择器
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取机构树选择器")
    @SaCheckPermission("/home/structure/user/orgTreeSelector")
    @GetMapping("/home/structure/user/orgTreeSelector")
    public CommonResult<List<Tree<String>>> orgTreeSelector() {
        return CommonResult.data(homeUserService.orgTreeSelector());
    }

    /**
     * 获取机构列表选择器
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取机构列表选择器")
    @SaCheckPermission("/home/structure/user/orgListSelector")
    @GetMapping("/home/structure/user/orgListSelector")
    public CommonResult<Page<HomeOrg>> orgListSelector(HomeUserSelectorOrgListParam homeUserSelectorOrgListParam) {
        return CommonResult.data(homeUserService.orgListSelector(homeUserSelectorOrgListParam));
    }

    /**
     * 获取岗位选择器
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取岗位选择器")
    @SaCheckPermission("/home/structure/user/positionSelector")
    @GetMapping("/home/structure/user/positionSelector")
    public CommonResult<Page<HomePosition>> positionSelector(HomeUserSelectorPositionParam homeUserSelectorPositionParam) {
        return CommonResult.data(homeUserService.positionSelector(homeUserSelectorPositionParam));
    }

    /**
     * 获取角色选择器
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取角色选择器")
    @SaCheckPermission("/home/structure/user/roleSelector")
    @GetMapping("/home/structure/user/roleSelector")
    public CommonResult<Page<HomeUserRoleResult>> roleSelector(HomeUserSelectorRoleParam homeUserSelectorRoleParam) {
        return CommonResult.data(homeUserService.roleSelector(homeUserSelectorRoleParam));
    }

    /**
     * 获取人员选择器
     *
     * @author xuyuxiang
     * @date 2022/4/24 20:00
     */
    @Operation(summary = "获取人员选择器")
    @SaCheckPermission("/home/structure/user/userSelector")
    @GetMapping("/home/structure/user/userSelector")
    public CommonResult<Page<HomeUser>> userSelector(HomeUserSelectorUserParam homeUserSelectorUserParam) {
        return CommonResult.data(homeUserService.userSelector(homeUserSelectorUserParam));
    }
}
